package com.A1908.servlet;

import com.A1908.entity.FeastInfo;
import com.A1908.entity.FeastInfoDataAnalysis;
import com.A1908.entity.PageEntity;
import com.A1908.entity.User;
import com.A1908.service.FeastInfoService;
import com.A1908.service.impl.FeastInfoServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/feast"})
public class FeastInfoServlet extends HttpServlet {
    FeastInfoService feastInfoService = new FeastInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String opr = request.getParameter("opr");

        PrintWriter out = response.getWriter();
        /*Ctrl + Alt + L 格式化代码*/
        if ("list".equals(opr) || opr == null) {
            PageEntity pageEntity = new PageEntity();
            pageEntity.setPageSize(Integer.parseInt(request.getParameter("limit")));
            pageEntity.setCurrentPage(Integer.parseInt(request.getParameter("page")));

            User user = new User();
            user.setId((int) request.getSession().getAttribute("userID"));
            List<FeastInfo> list = feastInfoService.findAllFeastInfo(user, pageEntity);
            if (list != null) {
                out.print(JSON.toJSONString(pageEntity));
            } else {
                out.print("false");
            }
        } else if ("add".equals(opr)) {
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            String date = request.getParameter("date");
            String cost = request.getParameter("cost");
            String address = request.getParameter("address");

            FeastInfo info = new FeastInfo();
            info.setFeastName(name);
            info.setFeastType(Integer.parseInt(type));
            info.setFeastDate(date);
            info.setFeastCost(Double.parseDouble(cost));
            info.setFeastAddress(address);
            info.setFeastUser((int) request.getSession().getAttribute("userID"));

            if (feastInfoService.addFeastInfo(info) > 0) {
                out.print("true");
            } else {
                out.print("false");
            }

        } else if ("edit".equals(opr)) {
            String fId = request.getParameter("fId");
            String fName = request.getParameter("fName");
            String fType = request.getParameter("fType");
            String fDate = request.getParameter("fDate");
            String fcost = request.getParameter("cost");
            String faddress = request.getParameter("address");

            FeastInfo info = new FeastInfo();
            info.setFeastName(fName);
            info.setFeastType(Integer.parseInt(fType));
            info.setFeastDate(fDate);
            info.setFeastCost(Double.parseDouble(fcost));
            info.setFeastAddress(faddress);
            info.setFeastUser((int) request.getSession().getAttribute("userID"));
            info.setId(Integer.parseInt(fId));

            if (feastInfoService.updateFeastInfo(info) > 0) {
                out.print("true");
            } else {
                out.print("false");
            }

        } else if ("findByName".equals(opr)) {
            PageEntity pageEntity = new PageEntity();
            pageEntity.setPageSize(Integer.parseInt(request.getParameter("limit")));
            pageEntity.setCurrentPage(Integer.parseInt(request.getParameter("page")));

            FeastInfo feastInfo = new FeastInfo();
            feastInfo.setFeastName(request.getParameter("fName"));

            if (request.getParameter("fType") != null && request.getParameter("fType") != "") {
                feastInfo.setFeastType(Integer.parseInt(request.getParameter("fType")));
            }

            String date = request.getParameter("fDate");
            
            if (date != null) {
                String[] strs = date.split(" ~ ");
                feastInfo.setStartDateAndEndDate(strs);
            }

            feastInfo.setFeastDate(request.getParameter("fDate"));
            feastInfo.setFeastUser((int) request.getSession().getAttribute("userID"));

            feastInfoService.findFeastInfoByName(feastInfo, pageEntity);
            out.print(JSON.toJSONString(pageEntity));
        } else if ("giftList".equals(opr)) {
            User user = new User();
            user.setId((int) request.getSession().getAttribute("userID"));
            FeastInfoDataAnalysis feastAnalysis = new FeastInfoServiceImpl().getFeastAnalysis(user);
            out.print(JSON.toJSONString(feastAnalysis));
        } else if ("del".equals(opr)) {
            FeastInfo feastInfo = new FeastInfo();
            feastInfo.setId(Integer.parseInt(request.getParameter("fId")));
            User user = new User();
            user.setId((int) request.getSession().getAttribute("userID"));
            int count = feastInfoService.delFeastInfo(feastInfo, user);
            if (count > 0) {
                out.print("true");
            } else {
                out.print("false");
            }
        }
        out.close();
    }
}
